<?php

/**
 *  +----------------------------------------------------------------------
 *  | 草帽支付系统 [ WE CAN DO IT JUST THINK ]
 *  +----------------------------------------------------------------------
 *  | Copyright (c) 2018 http://www.iredcap.cn All rights reserved.
 *  +----------------------------------------------------------------------
 *  | Licensed ( https://www.apache.org/licenses/LICENSE-2.0 )
 *  +----------------------------------------------------------------------
 *  | Author: Brian Waring <BrianWaring98@gmail.com>
 *  +----------------------------------------------------------------------
 */

namespace app\common\library;
use think\Cache;
use think\Env;
use think\Log;

/**
 * Class Activation
 *
 * @author 勇敢的小笨羊 <brianwaring98@gmail.com>
 *
 */
class Activation
{

    /**
     * 发送激活码链接   用户激活后在发送商户信息
     *
     * @author 勇敢的小笨羊 <brianwaring98@gmail.com>
     *
     * @param object $user
     * @return bool|string
     */
    public function sendActiveCode($user){
        //收件人邮箱
        $toemail    =   $user->account;
        //发件人昵称
        $name       =   !empty($user->nickname)? $user->nickname:'小余';
        //邮件标题
        $subject    =   "感谢您申请余呗聚合支付";
        //生成code
        $activecode = urlencode($this->createActiveCode($user));
        //激活地址
        $activeUrl = "https://www.". Env::get('app.domain_root','iredcap.com')
            ."/active/{$activecode}";
        //邮件主体  也可以使用邮件模板文件
        $content    =   "<div style=\"margin: -15px; padding: 8vh 0 2vh;color: #a6aeb3; background-color: #f7f9fa; text-align: center; font-family:NotoSansHans-Regular,'Microsoft YaHei',Arial,sans-serif; -webkit-font-smoothing: antialiased;\">
                            <div style=\"width: 750px; margin: 0 auto; background-color: #fff;\">
                                <div style=\"padding: 20px 10%; text-align: center; font-size: 16px;line-height: 16px;\">
                                    <a href=\"https://www.98imo.com\" style=\"vertical-align: top;\" target=\"_blank\"> <img style=\"margin:32px auto; max-width: 95%; color: #0e2026;\" src=\"https://www.98imo.com/assets/logo-black.png\" /> </a>
                                </div>
                                <table width=\"600\" style=\"background-color:#fff;margin:0 auto;\" cellpadding=\"0\" cellspacing=\"0\">
                                    <tbody><tr>
                                        <td>
                                            <table width=\"600\" style=\"background-color:#fff;margin:0 auto;\" cellpadding=\"0\" cellspacing=\"0\">
                                                <tbody>
                                                <tr>
                                                    <td colspan=\"3\" style=\"height:40px;\">&nbsp;</td>
                                                </tr>
                                                <tr>
                                                    <td width=\"40\">&nbsp;</td>
                                                    <td width=\"520\" style=\"line-height:20px;\">
                                                        <p style=\"text-align:center;margin:0;padding:0;\">
                                                            <img src=\"https://img.alicdn.com/tfs/TB1yM1Pb4GYBuNjy0FnXXX5lpXa-64-64.png\" width=\"32\" height=\"32\" style=\"margin:0 12px;vertical-align:top;\">
                                                            <span style=\"font-size:24px;line-height:32px;color:#35B34A;\">注册申请成功！</span>
                                                        </p>
                                                        <p style=\"color:#7d7d7d;margin:10px 0px 24px 0px;font-size:14px;line-height:22px;padding:0 40px;text-align:center\">欢迎{$name}加入,在开始使用之前，请确认你的邮箱账号
                                                        </p>
                                                      
                                                        <p style=\"margin:0;padding:0;\">&nbsp;</p>
                                                        <table align=\"center\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"height:44px; width:200px\">
                                                            <tbody>
                                                            <tr>
                                                                <td style=\"background-color:#00c1de; height:44px; line-height:44px;text-align:center; width:200px\">
                                                                    <a href=\"{$activeUrl}\" style=\"display:block;text-decoration: none;color: #ffffff;font-size:16px;\" target=\"_blank\">激活账户
                                                                    </a>
                                                                </td>
                                                            </tr>
                                                            </tbody>
                                                        </table>
                                                    </td>
                                                    <td width=\"40\">&nbsp;</td>
                                                </tr>
                                                <tr>
                                                    <td colspan=\"3\" style=\"height:40px;\">
                                                        <div style=\"width: 500px; max-width: 90%;margin: 10px auto; font-size: 14px;\">
                                                            <div style=\"color:#7d7d7d;margin: 8px 0;\">
                                                                如果按钮无效，请将以下链接复制到浏览器地址栏完成激活。
                                                            </div>
                                                            <div>
                                                                <a href=\"{$activeUrl}\" style=\"color: #35c8e6; word-break: break-all\" target=\"_blank\">{$activeUrl}</a>
                                                            </div>
                                                        </div>
                                                    </td>
                                                </tr>
                                                </tbody>
                                            </table>
                                        </td>
                                    </tr>
                                    </tbody></table>
                                <div style=\"padding-bottom: 40px;font-size: 14px;\">
                                    <div style=\"padding-bottom: 40px;font-size: 14px;\">
                                        <div style=\"width: 420px; max-width: 90%;margin: 10px auto;\">
                                            彻底告别繁琐的支付接入流程 一次接入所有主流支付渠道，99% 系统可用性，满足你丰富的交易场景需求,为你的用户提供完美支付体验。
                                        </div>
                                        <div style=\"margin: 30px 0 0 0;\">
                                            扫码关注 了解最新动态
                                        </div>
                                        <div style=\"margin: 16px 0 32px;\">
                                            <img height=\"119\" src=\"\" width=\"119\" />
                                        </div>
                                        <div>
                                            <span style=\"color: #76858c;\">服务咨询请联系：</span>
                                            <a href=\"mailto:me@iredcap.cn\" style=\"color:#35c8e6; text-decoration: none;\" target=\"_blank\"> me@iredcap.cn </a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>";
        //发送激活邮件
        try{
            Mail::getInstance(config('code.Email'))->send($toemail,$name,$subject,$content);
            return $activecode;
        }catch (\Exception $exception){
            Log::error("Active Code Error:".$exception->getMessage());
            return false;
        }
    }

    /**
     * 生成用户激活码
     * @param $userInfo
     * @return string
     */
    public function createActiveCode($userInfo){
        // 机密数据
        $data = [
            'user' =>  obj2arr($userInfo),
            'noncestr' => getRandChar('32')
        ];
        //生成Code
        $code = data_md5(json_encode($data));
        //加密
        //1.用户激活的时候返回 code
        //2.用code解密取到ActiveCode的数据
        //3.比对cache
        //4.完成激活
        $encryptData = (new Aes($code))->encrypt(json_encode(obj2arr($userInfo))); //用户信息
        //存入cache 2H内验证时间 2*60*60 7200秒
        Cache::set('avtive_code_'.$code,$encryptData,7200);
        return $code;
    }

    /**
     * 用户激活码验证
     * @param $code
     * @return object|bool
     */
    public function VerificationActiveCode($code){

        //获取缓存的值
        $build = 'avtive_code_'.$code;
        $activeCode = Cache::get($build);
        //判断缓存是否过期
        if(empty( $activeCode )){
            return false;
        }else{
            //删除Cache
            //Cache::rm($build);
            $user = (new Aes($code))->decrypt($activeCode);
            return json_decode($user);
        }
    }
}